<?php
namespace Home\Controller;

use Think\Controller;

class IndexController extends Controller
{
    public function index(){
        $goods = D('Goods');
        $info = $goods->order('browse_num desc')->limit(8)->select();
        foreach ($info as &$val){
            $val['category_name'] = $val['user_judge'] == 'need' ? '需求' : '服务';
        }
        // dump($info);
        $this->assign('info', $info);
        $this->display();
    }

    public function getIndexData(){
        $page = $_POST['page'];
        $start = $page*8;
        $limit = $start.',8';

        $order = 'browse_num desc';
        $info = D('Goods')->order($order)->limit($limit)->select();
        foreach ($info as &$val){
            $val['category_name'] = $val['user_judge'] == 'need' ? '需求' : '服务';
        }
        if ($info) {           
            echo json_encode($info);
        } else {
            echo json_encode(['state' => -1]);
        }
    }

    public function getData(){
        $page = $_POST['page'];
        $start = $page*8;
        $limit = $start.',8';

        $where = [];
        if ($_POST['search']) {
            if($_POST['search_option'] == 1){ //信息
            $where = [
                'goods_title'=>['like','%'.$_POST['search'].'%']
            ];
            }else{ //用户
                $user_id = M('user')->where(['user_name'=>$_POST['search']])->getField('id');
                $where = ['user_id'=>$user_id];
            }
        }
        
        if ($_POST['module'] == 10) { // 热门
            $order = 'browse_num desc';
        } else {
            $where = ['goods_sort' => $_POST['module']];
            $order = 'id desc';
        }
        if ($_POST['cate'] != 'all') {
            $where['user_judge'] = $_POST['cate'];
        }
        $info = D('Goods')->where($where)->order($order)->limit($limit)->select();
        foreach ($info as &$val){
            $val['category_name'] = $val['user_judge'] == 'need' ? '需求' : '服务';
        }
        if ($info) {
            
            echo json_encode($info);
        } else {
            echo json_encode(['state' => -1]);
        }
    }


    public function test(){

        $goods = D('Goods');
        $info = $goods->order('browse_num desc')->limit(3)->select();
        foreach ($info as &$val){
            $val['category_name'] = $val['user_judge'] == 'need' ? '需求' : '服务';
        }
        // dump($info);
        $this->assign('info', $info);
        $this ->display();
    }

}